package io.renren.modules.substation.service.impl;

import io.renren.modules.substation.entity.TSampThDataInfoHisEntity;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.substation.dao.TSampJfDataInfoHisDao;
import io.renren.modules.substation.entity.TSampJfDataInfoHisEntity;
import io.renren.modules.substation.service.TSampJfDataInfoHisService;

import javax.annotation.Resource;


@Service("tSampJfDataInfoHisService")
public class TSampJfDataInfoHisServiceImpl extends ServiceImpl<TSampJfDataInfoHisDao, TSampJfDataInfoHisEntity> implements TSampJfDataInfoHisService {
    @Resource
    private TSampJfDataInfoHisDao tSampJfDataInfoHisDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TSampJfDataInfoHisEntity> page = this.page(
                new Query<TSampJfDataInfoHisEntity>().getPage(params),
                new QueryWrapper<TSampJfDataInfoHisEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取局放设备图表
     *
     * @return 局放设备图表
     *         averageAxis: 均值轴
     *         maxAxis: 最大轴
     *         phaseAxis: 频次轴
     */
    @Override
    public Map<String, List<?>> chartJf() {
        List<TSampJfDataInfoHisEntity> list = tSampJfDataInfoHisDao.get24Data();

        // 结果数据
        Map<Long, Map<String, Float>> resultMap = new LinkedHashMap<>();

        // 获取24小时前的时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        long oneHourMillis = 1000L * 60 * 60;
        while (calendar.getTimeInMillis() < System.currentTimeMillis() - oneHourMillis) {
            // 获取起始时间和结束时间
            long startMillis = calendar.getTimeInMillis();
            calendar.add(Calendar.HOUR_OF_DAY, 1);
            long endMillis = calendar.getTimeInMillis();

            // 获取该时间段的平均值
            float average = 0;
            float max = 0;
            float phase = 0;
            int valueCount = 0;
            for (TSampJfDataInfoHisEntity sampJfDataInfoHisEntity : list) {
                if (sampJfDataInfoHisEntity.getCreateTime() == null) {
                    continue;
                }

                // 如果在当前小时呢
                long createTime = sampJfDataInfoHisEntity.getCreateTime().getTime();
                if (createTime > startMillis && createTime <= endMillis) {
                    average += Float.parseFloat(sampJfDataInfoHisEntity.getAvDsch());
                    max += Float.parseFloat(sampJfDataInfoHisEntity.getMaxDsch());
                    phase += Float.parseFloat(sampJfDataInfoHisEntity.getDschPhase());
                    valueCount++;
                }
            }

            // 计算平均值
            average = valueCount == 0 ? average : average / valueCount;
            max = max == 0 ? max : max / valueCount;
            phase = phase == 0 ? phase : phase / valueCount;
            // 精确1为小数
            DecimalFormat decimalFormat = new DecimalFormat("#.#");
            average = Float.parseFloat(decimalFormat.format(average));
            max = Float.parseFloat(decimalFormat.format(max));
            phase = Float.parseFloat(decimalFormat.format(phase));

            // 封装当前小时的数据
            Map<String, Float> hourMap = new HashMap<>(2);
            hourMap.put("average", average);
            hourMap.put("max", max);
            hourMap.put("phase", phase);
            resultMap.put(endMillis, hourMap);
        }

        return new HashMap<String, List<?>>(4) {{
            // x轴
            List<String> xAxis = new LinkedList<>();
            // y轴
            List<Float> averageAxis = new LinkedList<>();
            List<Float> maxAxis = new LinkedList<>();
            List<Float> phaseAxis = new LinkedList<>();

            // 取数据
            resultMap.forEach((millis, hourMap) -> {
                DateFormat dateFormat = new SimpleDateFormat("d日H时");
                xAxis.add(dateFormat.format(new Date(millis)));
                averageAxis.add(hourMap.get("average"));
                maxAxis.add(hourMap.get("max"));
                phaseAxis.add(hourMap.get("phase"));
            });

            put("xAxis", xAxis);
            put("averageAxis", averageAxis);
            put("maxAxis", maxAxis);
            put("phaseAxis", phaseAxis);
            put("type", new ArrayList<String>(2) {{
                add("均值");
                add("峰值");
                add("频次");
            }});
        }};
    }
}